package com.newsmanagement.service.impl;

import com.newsmanagement.common.Constants;
import com.newsmanagement.dao.IEumRoleServiceDao;
import com.newsmanagement.dao.IEumServiceDao;
import com.newsmanagement.entity.EumEntity;
import com.newsmanagement.entity.ReturnedMessages;
import com.newsmanagement.service.IEumService;
import com.newsmanagement.util.EncryptionMd5;
import com.newsmanagement.util.EntityMapChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author xbr
 * 2017/12/4 16:29
 * 用户管理操作
 */
@Service
@Transactional
public class EumServiceImpl implements IEumService {


    @Autowired
    private IEumServiceDao eumServiceDao;

    @Autowired
    private IEumRoleServiceDao eumRoleServiceDao;

    /**
     * 添加用户
     * @param eumName 用户名称，可不填
     * @param mobilePhone 手机号码，必填
     * @param password 密码，可不填，默认会自动插入111111
     * @return ReturnedMessages
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnedMessages addEum(String eumName,String mobilePhone,String password){

        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证手机号码
        if(null == mobilePhone || mobilePhone.trim().length() < Constants.INT_ONE ){
            returnedMessages.setCode(500);
            returnedMessages.setMessage("用户手机号码传递错误：号码为空");

            return returnedMessages;
        }
        mobilePhone = mobilePhone.trim();

        //用户名验证
        if(null != eumName && eumName.trim().length() > Constants.INT_ZERO ){
            eumName = eumName.trim();
        }else{
            eumName = "";
        }

        //密码验证
        if(null != password && password.trim().length() > Constants.INT_ZERO ){
            //用md5对密码进行加密
            password = EncryptionMd5.stringMD5(password.trim());
        }else{
            password = EncryptionMd5.stringMD5(Constants.PASSWORD);
        }

        /*
         * 根据手机号获取数据库中的用户信息，
         * 如果存在未删除则不予以添加，也不进行修改
         * 如果存在已删除则不予以添加，进行修改信息
         * 如果不存在则进行添加
         */
        List<Map<String, Object>> eumList =  eumServiceDao.getEumByMobilePhone(mobilePhone);

        String deleteKey = "DELETE_FLAG";
        if (null != eumList && eumList.size() > Constants.INT_ZERO){
            //存在

            //存在多个，数据库数据错误
            if (eumList.size() > Constants.INT_ONE){
                returnedMessages.setCode(400);
                returnedMessages.setMessage("数据错误：数据库中存在多个相同手机号的账户");
                return returnedMessages;
            }

            //存在一个，获取对应的用户数据
            Map<String, Object> eum = eumList.get(Constants.INT_ZERO);

            if (null != eum && eum.get(deleteKey).equals(Constants.STRING_ZERO)){
                //存在已删除则不予以添加，进行修改信息

                //根据用户手机号修改对应的用户信息
                int updateEumNum = eumServiceDao.updateEumByMobilePhone(eumName ,mobilePhone ,password);
                if (updateEumNum == Constants.INT_ZERO){
                    returnedMessages.setCode(300);
                    returnedMessages.setMessage("根据用户手机号未查找到对应的用户信息");
                }else{
                    returnedMessages.setData(updateEumNum);
                    return returnedMessages;
                }

            }else if (null != eum && eum.get(deleteKey).equals(Constants.STRING_ONE)){
                //存在未删除则不予以添加，也不进行修改

                returnedMessages.setCode(400);
                returnedMessages.setMessage("手机号已经存在，请重新选择");
                return returnedMessages;
            }
        }
        //不存在,进行添加
        //添加用户
        int addNum= eumServiceDao.addEum(eumName ,mobilePhone ,password);
        if (addNum < Constants.INT_ONE){
            returnedMessages.setCode(400);
            returnedMessages.setMessage("用户保存未成功");
        }

        return returnedMessages;
    }

    /**
     * 删除用户
     * @param eumId 用户id
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages deleteEum(Long eumId){

        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证手机号码
        if(null != eumId && eumId > Constants.LONG_ZERO ){
            int deleteNum = eumServiceDao.deleteEum(eumId);
            if (deleteNum == Constants.INT_ZERO){
                returnedMessages.setCode(300);
                returnedMessages.setMessage("根据用户主键id未查找到对应的用户信息");
            }else{
                returnedMessages.setData(deleteNum);
            }
        }else{
            returnedMessages.setCode(500);
            returnedMessages.setMessage("用户主键传递错误：主键为空");
        }

        return returnedMessages;
    }

    /**
     * 用户登录
     * @param mobilePhone 手机号码
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages login(HttpServletRequest request , String mobilePhone ,String password){

        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证手机号码
        if(null == mobilePhone || mobilePhone.trim().length() < 1 ){
            return  returnedMessages.ReturnedMessagesFail(500 ,"请输入手机号");
        }

        //验证手机号码
        if(null == password || password.trim().length() < 1 ){
            return  returnedMessages.ReturnedMessagesFail(500 ,"请输入密码");
        }
        password = EncryptionMd5.stringMD5(password);

        System.out.println(password);
        //查询用户信息是否存在
        List<Map<String, Object>> list = eumServiceDao.getEumCheckLogin(mobilePhone ,password);
        if(null == list || list.size() < 1 || null == list.get(0)){
            return returnedMessages.ReturnedMessagesFail(400 ,"手机号或者密码错误");
        }
        Map<String, Object> map = list.get(0);

        EumEntity eumEntity = EntityMapChange.mapToEntity(map, EumEntity.class);
        returnedMessages.setData(eumEntity);

        request.getSession().setAttribute("eum", eumEntity);

        return returnedMessages;
    }


    /**
     * 设置用户对应角色
     * @param eumId 用户ID
     * @param roleId 角色ID
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages eumToRole(Integer eumId, Integer roleId) {

        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证用户主键
        if(null == eumId || eumId < Constants.INT_ONE ){
            returnedMessages.setCode(500);
            returnedMessages.setMessage("用户主键传递错误：主键为空");
        }

        //验证角色主键
        if(null == roleId || roleId < Constants.INT_ONE ){
            returnedMessages.setCode(500);
            returnedMessages.setMessage("角色主键传递错误：主键为空");
        }

        eumRoleServiceDao.add(eumId, roleId);
        return returnedMessages;
    }


}
